package com.jiaxue.admin.controller;

import javax.servlet.http.HttpSession;

import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiaxue.admin.model.Admin;
import com.jiaxue.admin.model.ReturnObj;
import com.jiaxue.admin.service.LoginService;


@Controller 
public class LoginController {
	
	@Autowired
	private LoginService loginService;
	
	@RequestMapping(value="/login")
	public @ResponseBody  JSONPObject  login(
			@RequestParam String name,
			@RequestParam String password,
			@RequestParam String callback,
			HttpSession session){
		ReturnObj roObj=null;
		try{
			Admin admin=new Admin();
			admin.setName(name);
			admin.setPassword(password);
			Admin ad = loginService.login(admin);
			if(ad!=null){
				session.setAttribute("loginUser", ad);
				roObj=new ReturnObj(200, "登陆成功",ad);
			}else{
				roObj=new ReturnObj(-1, "用户名或者密码错误",null);
			}
		}catch(Exception e){
			e.printStackTrace();
			roObj=new ReturnObj(-1, e.getMessage(),null);
		}
		return new JSONPObject(callback, roObj);
	}
}
